package propert_scence

import (
	"clientDemo/ui/fyne_ui/widget/custom"
	widget2 "clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

const (
	DefaultEditorName = "常规"
)

func NewPropertiesEditor(name string) *PropertiesEditor {
	o := &PropertiesEditor{
		name: name,
	}
	o.tabItem = &container.TabItem{}
	o.tabItem.Text = o.name

	o.editorAccordion = widget.NewAccordion()
	o.mainScroll = container.NewScroll(
		container.NewVBox(
			o.editorAccordion,
			layout.NewSpacer(),
		))
	o.tabItem.Content = o.mainScroll

	item := widget.NewAccordionItem(DefaultEditorName, container.NewVBox())
	o.editorAccordion.Append(item)
	o.accordionItems = map[string]*widget.AccordionItem{}
	o.accordionItems[item.Title] = item
	return o
}

type PropertiesEditor struct {
	tabItem         *container.TabItem // 页签
	mainScroll      *container.Scroll  // 页签下滚动页面
	editorAccordion *widget.Accordion  // 可折叠 zhu节点
	accordionItems  map[string]*widget.AccordionItem
	name            string
	currentObj      widget2.IWidgetItem //当前编辑的对象
}

func (p *PropertiesEditor) GetTabItem() *container.TabItem {
	return p.tabItem
}

func (p *PropertiesEditor) GetEditorObj() *widget.Accordion {
	return p.editorAccordion
}
func (p *PropertiesEditor) ClearEditorAccordion() {
	p.editorAccordion.CloseAll()
}
func (p *PropertiesEditor) UpdateAccordionDefault(entryName string, value string, readOnly bool) {
	p.UpdateAccordionByName(DefaultEditorName, entryName, value, readOnly)
}
func (p *PropertiesEditor) ClearEditorAccordionItemDefault() {
	p.ClearEditorAccordionItem(DefaultEditorName)
	return
}
func (p *PropertiesEditor) ClearEditorAccordionItem(title string) {
	var selectItem = p.getAccordionItemByName(title)
	if selectItem == nil {
		return
	}
	vbox, ok := selectItem.Detail.(*fyne.Container)
	if !ok {
		return
	}
	vbox.RemoveAll()
	vbox.Refresh()
	return
}
func (p *PropertiesEditor) getAccordionItemByName(title string) *widget.AccordionItem {
	var selectItem *widget.AccordionItem
	for i := 0; i < len(p.editorAccordion.Items); i++ {
		if p.editorAccordion.Items[i].Title == title {
			selectItem = p.editorAccordion.Items[i]
			break
		}
	}
	return selectItem
}
func (p *PropertiesEditor) UpdateAccordionByName(title string, entryName string, value string, readOnly bool) {
	var selectItem = p.getAccordionItemByName(title)
	if selectItem == nil {
		return
	}
	vbox, ok := selectItem.Detail.(*fyne.Container)
	if !ok {
		return
	}
	if readOnly {
		entryControl := container.New(
			layout.NewGridLayout(2),
			widget.NewLabel(entryName),
			widget.NewLabel(value),
		)
		vbox.Add(entryControl)
	} else {
		if entryName == "Scale" {
			editSlider := custom.NewEditingOptionWithSlider(entryName, 0.1, 2, 1, func(f float64) {
				if entryName == "Scale" {
					p.currentObj.SetProperty(entryName, utils.ToString(f), true)
				}
				fmt.Println("+++++++++++++++++++++++++")
			})
			vbox.Add(editSlider)
		}

		entry := widget.NewEntry()
		entry.SetText(value)
		//entry.SetPlaceHolder(value)
		entryControl := container.New(
			layout.NewGridLayout(2),
			widget.NewLabel(entryName),
			entry)
		vbox.Add(entryControl)

		entry.OnChanged = p.entryChange(entryName)
	}
	vbox.Refresh()
}

func (p *PropertiesEditor) entryChange(entryName string) func(s string) {
	return func(s string) {
		if p.currentObj == nil {
			return
		}
		p.currentObj.SetProperty(entryName, s, true)
	}
}
func (p *PropertiesEditor) UpdateAccordionWidgetItem(obj widget2.IWidgetItem, pos fyne.Position) {
	p.currentObj = obj
	p.ClearEditorAccordionItemDefault()
	p.UpdateAccordionDefault("模板名称", obj.(widget2.IUserInfo).GetSkinTempName(), true)
	p.UpdateAccordionDefault("控件名称", obj.GetName(), false)
	p.UpdateAccordionDefault("点击位置X", utils.ToString(pos.X), false)
	p.UpdateAccordionDefault("点击位置Y", utils.ToString(pos.Y), false)
	obj.(widget2.IUserInfo).ForEachProperty(func(key, value string) {
		p.UpdateAccordionDefault(key, value, false)
	})
}

var EditorAcc fyne.CanvasObject
var AccordionItemGroup = map[string][]fyne.CanvasObject{}
var AccordionItems = map[string]*widget.AccordionItem{}

// loadEditorTab returns the editor tab
func loadEditorTab() *container.TabItem {
	editColorBalanceR := custom.NewEditingOptionWithSlider("Red: ", -100, 500, 0, nil)
	editSepia := custom.NewEditingOptionWithSlider("Sepia: ", 0, 100, 0, func(f float64) {
		fmt.Println("+++++++++++++++++++++++++")
	})

	editBlur := custom.NewEditingOptionWithSlider("Blur: ", 0, 100, 0, nil)

	rotate90Btn := widget.NewButton("Rotate 90°", func() {})
	resizeBtn := widget.NewButton("Resize", func() {
	})

	moreEvilNinjas := []string{".aac", ".aiff", ".avi", ".bmp", ".c", ".cpp", ".csv", ".dat", ".dmg", ".dll", ".dng", ".doc", ".docx", ".eps", ".exe", ".flac", ".gif", ".h", ".html", ".ics", ".iso", ".java", ".jpeg", ".jpg", ".json", ".key", ".log", ".m4a", ".mp3", ".mp4", ".mpg", ".odt", ".otf", ".pdf", ".png", ".ppt", ".pptx", ".psd", ".py", ".rar", ".raw", ".rtf", ".svg", ".tar", ".tex", ".tga", ".tif", ".tiff", ".ts", ".txt", ".wav", ".webm", ".wmv", ".xls", ".xlsx", ".xml", ".yaml", ".zip"}
	folder_type := widget.NewSelectEntry(moreEvilNinjas)
	folder_type.SetPlaceHolder("Enter file type (ex .jpg)")
	folder_name := widget.NewEntry()
	folder_name.SetPlaceHolder("Enter folder name")
	folder_type.OnChanged = func(s string) {
		fmt.Println("--------------------------")
	}
	folder_name.OnChanged = func(s string) {
		fmt.Println("+++++++++++++++++++++++++")
	}
	// create grid
	one_folder := container.New(layout.NewGridLayout(2), folder_type, folder_name)
	one_folder2 := container.New(layout.NewGridLayout(3), widget.NewLabel("你好1"), widget.NewLabel("你好1"), widget.NewEntry())
	grid := container.New(layout.NewGridLayout(1), one_folder, one_folder2)
	AccordionItemGroup["General"] = []fyne.CanvasObject{
		container.NewVBox(
			editSepia,
			editBlur,
		),
	}
	AccordionItemGroup["常规"] = []fyne.CanvasObject{
		container.NewVBox(
			grid,
		),
	}
	AccordionItemGroup["Color Balance"] = []fyne.CanvasObject{
		container.NewVBox(
			editColorBalanceR,
		),
	}
	AccordionItemGroup["Transform"] = []fyne.CanvasObject{
		container.NewVBox(
			rotate90Btn,
			resizeBtn,
		),
	}
	accordion := widget.NewAccordion()
	for s, objects := range AccordionItemGroup {
		a := widget.NewAccordionItem(s, container.NewVBox(objects...))
		AccordionItems[s] = a
		accordion.Append(a)
	}
	EditorAcc = accordion
	scroll := container.NewScroll(
		container.NewVBox(
			EditorAcc,
			layout.NewSpacer(),
		))
	return container.NewTabItem("属性", scroll)
}
func AddEditorAccordionItem(title string) bool {
	if EditorAcc == nil {
		return false
	}
	adItems, ok := AccordionItems[title]
	if !ok {
		return false
	}
	vbox, ok := adItems.Detail.(*fyne.Container)
	if !ok {
		return false
	}
	edit2 := custom.NewEditingOptionWithSlider("Red2: ", -100, 500, 0, nil)
	vbox.Add(edit2)
	vbox.Refresh()
	return true
}
func AddEditorAccordionItemEntry(title string, entryName string, defaultName string, readOnly bool) bool {
	if EditorAcc == nil {
		return false
	}
	adItems, ok := AccordionItems[title]
	if !ok {
		return false
	}
	vbox, ok := adItems.Detail.(*fyne.Container)
	if !ok {
		return false
	}
	if readOnly {
		entryControl := container.New(
			layout.NewGridLayout(2),
			widget.NewLabel(entryName),
			widget.NewLabel(defaultName),
		)
		vbox.Add(entryControl)
	} else {
		entry := widget.NewEntry()
		entry.SetText(defaultName)
		//entry.SetPlaceHolder(defaultName)
		entryControl := container.New(
			layout.NewGridLayout(2),
			widget.NewLabel(entryName),
			entry)
		vbox.Add(entryControl)
	}
	vbox.Refresh()
	return true
}
func ClearEditorAccordionItem(title string) {
	if EditorAcc == nil {
		return
	}
	adItems, ok := AccordionItems[title]
	if !ok {
		return
	}
	vbox, ok := adItems.Detail.(*fyne.Container)
	if !ok {
		return
	}
	vbox.RemoveAll()
	vbox.Refresh()
	return
}
